package edu.scau.mis.system.service.impl;

import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import edu.scau.mis.system.domain.Menu;
import edu.scau.mis.system.domain.Role;
import edu.scau.mis.system.domain.RoleMenu;
import edu.scau.mis.system.mapper.MenuMapper;
import edu.scau.mis.system.mapper.RoleMapper;
import edu.scau.mis.system.mapper.RoleMenuMapper;
import edu.scau.mis.system.mapper.UserRoleMapper;
import edu.scau.mis.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    /**
     * 根据id查询role
     *
     * @param roleId
     * @return
     */
    @Override
    public Role selectRoleById(Long roleId) {
        return roleMapper.selectRoleById(roleId);
    }

    /**
     * 查询role列表
     *
     * @param role
     * @return
     */
    @Override
    public List<Role> selectRoleList(Role role) {
        return roleMapper.selectRoleList(role);
    }

    /**
     * 查询所有role
     * @return
     */
    @Override
    public List<Role> selectAllRole(){
        return roleMapper.selectAllRole();
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @Override
    public int deleteRoleById(Long roleId) {
        return roleMapper.deleteRoleById(roleId);
    }

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @Override
    public int insertRole(Role role) {
        return roleMapper.insertRole(role);
    }

    /**
     * 更新角色
     *
     * @param role
     * @return
     */
    @Override
    public int updateRole(Role role) {
        return roleMapper.updateRole(role);
    }

    @Override
    @Transactional  // 添加事务注解，确保批量操作原子性
    public int batchRoleMenu(RoleMenu roleMenu) {
        // 先delete再插入
        roleMenuMapper. deleteRoleMenuByRoleId(roleMenu.getRoleId());
        // 1. 参数校验
        System.out.println(roleMenu.getMenuIds());
        if (CollectionUtils.isEmpty(roleMenu.getMenuIds())) {
            throw new IllegalArgumentException("角色菜单列表不能为空");
        }
        // 2. 调用Mapper执行批量插入
        return roleMenuMapper.batchRoleMenu(roleMenu);
    }

    @Override
    public List<Menu> selectMenusByRoleId(Long roleId) {
        return menuMapper.selectMenusByRoleId(roleId);
    }
    @Override
    public List<String> selectRoleKeysByUserId(Long userId) {
        //查询用户拥有的角色role_key列表
        List<String> roleKeys = userRoleMapper. selectRoleKeysByUserId(userId);
        return roleKeys;
    }
}
